<template>
  <div></div>
</template>
<script lang="ts">
import { defineComponent, ref } from "vue";
import { storageLocal } from "/@/utils/storage";
import { useRouter } from "vue-router";
import axios from "axios";
import NotifMessage from "/@/utils/message/notification";

export default defineComponent({
  name: "logout",
  setup() {
    const router = useRouter();
    const baseUrl = ref(import.meta.env.VITE_BASE_URL);

    const initToLogout = async () => {
      let authorization = "";
      if (storageLocal.getItem("info")) {
        authorization = storageLocal.getItem("info")?.accessToken;
      } else {
        router.push({ path: "/login" });
      }
      const loginUrl = `${baseUrl.value}/snow-admin/logout`;
      try {
        const res = await axios.post(
          loginUrl,
          {},
          {
            headers: {
              Authorization: authorization,
              loginMethod: "PC"
            }
          }
        );
        if (res.data?.body?.code === 200) {
          NotifMessage.success("注销登录成功");
          storageLocal.removeItem("info");
          router.push({ path: "/login" });
        } else {
          storageLocal.removeItem("info");
          router.push({ path: "/login" });
        }
      } catch (err) {
        storageLocal.removeItem("info");
        router.push({ path: "/login" });
        console.log(err);
      }
    };

    initToLogout();

    return {};
  }
});
</script>
